package com.googlecode.recycled.xml;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import java.util.List;

public class BufferedErrorHandler implements ErrorHandler {

    protected List<SAXParseException> parseExceptions = Lists.newLinkedList();

    @Override
    public void warning(SAXParseException e) throws SAXException {
        parseExceptions.add(e);
    }

    @Override
    public void error(SAXParseException e) throws SAXException {
        parseExceptions.add(e);
    }

    @Override
    public void fatalError(SAXParseException e) throws SAXException {
        parseExceptions.add(e);
    }

    public List<SAXParseException> getParseExceptions() {
        return Lists.newLinkedList(parseExceptions);
    }

    public static class EXCEPTION_TO_MESSAGE implements Function<SAXParseException, String> {

        @Override
        public String apply(SAXParseException from) {
            return from.getMessage();
        }

    }

}
